<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<script>
    const inject = {
        dependencies: {},
        register: function (key, value) {
            this.dependencies[key] = value;
        },
        resolve: function (func, scope) {
            let deps;
            // 如果传入的函数是一个数组，大概长这样[a,b,function(a,b){}]，那么我们删掉function(){}这部分，只留下我们的依赖部分,这样就解决了打包压缩时的问题
            if (Array.isArray(func)) {
                let last = func.length - 1;
                console.log(last)
                deps = func.slice(0, last);
                console.log(deps)
                func = func[last]


            } else {
                let FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m;
                let STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
                let fnText = func.toString().replace(STRIP_COMMENTS, '');
                let argDecl = fnText.match(FN_ARGS);
                deps = argDecl[1].split(',');
            }
            let arr = [];
            for (let i = 0; i < deps.length; i++) {
                if (this.dependencies.hasOwnProperty(deps[i])) {
                    arr.push(this.dependencies[deps[i]])
                }
            }
            return function () {
                func.apply(scope || {}, arr);
            }
        }
    }


    inject.register('$http', {
        'get': function () {
            return '我是依赖注入的$http下的一个函数';
        }
    });
    inject.register('$scope', {
        'test': ''
    });
    let MyController = ['$scope', '$http', function ($scope, $http) {
        console.log(`MyController-result:${$http.get()}`)
    }];
    MyController = inject.resolve(MyController)
    MyController(); // MyController-result:我是依赖注入的$http下的一个函数
</script>
</body>
</html>